//
//  NetTool.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "NetTool.h"

@implementation NetTool
+(void)GET:(NSString *)url body:(id)body response:(TYResponseStyle)responseStyle headerFile:(NSDictionary *)headers success:(successBlock)success failure:(failureBlock)failure{
    //1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2.设置请求头
    if (headers) {
        for (NSString *key in headers.allKeys) {
            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];
        }
    }
    //3.设置返回数据类型
    switch (responseStyle) {
        case FVJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case FVXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case FVDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //4.设置响应数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //5.UTF8转码
        url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //使用AFN进行网络请求
    
    [manager GET:url parameters:body progress:nil
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             if (responseObject) {
                 success(responseObject);
             }
             
         }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             if (error) {
                 failure(error);
             }
             
         }];
    
    
    
}

+(void)POST:(NSString *)url
       body:(id)body
  bodtStyle:(TYRequestStyle)bodyStyle
   response:(TYResponseStyle)responseStyle
 headerFile:(NSDictionary *)headers
    success:(successBlock)success
    failure:(failureBlock)failure{
    //1.设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2.设置body数据类型
    switch (bodyStyle) {
        case FVBodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case FVBodyString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    //3.设置请求头
    if (headers) {
        for (NSString *key in headers.allKeys) {
            [manager.requestSerializer setValue:headers[key] forHTTPHeaderField:key];
        }
    }
    //4.设置返回数据类型
    switch (responseStyle) {
        case FVJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case FVXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case FVDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //5.设置响应数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //6.UTF8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //7.使用AFN进行网络请求
    
    [manager POST:url parameters:body progress:nil
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              if (responseObject) {
                  success(responseObject);
              }
              
          }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              if (error) {
                  failure(error);
              }
              
          }];
    
    
    
    
}


@end
